/*
 * GUISeleccionTop.java
 *
 * Created on 10 de mayo de 2010, 18:34
 */

package interfaz;

import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 * Clase para la elección de TOP entre varios VHD
 *
 * @author  David,Carlos y Tony
 */
public class GUISeleccionTop extends javax.swing.JDialog {

    /**
     * Conjunto de ficheros VHD
     */
    private ArrayList<String> ficheros;

    /**
     * Componente que hace la llamada
     */
    private JFrame padre;
    
    /** Constructor de form GUISeleccionTop
     * @param parent Frame padre encargado de la llamada.
     * @param modal  Indica si es modal o no.
     * @param fich   ArrayList de ficheros seleccionados.
     */
    @SuppressWarnings("empty-statement")
    public GUISeleccionTop(java.awt.Frame parent, boolean modal,ArrayList<String> fich) {
        super(parent, modal);
        initComponents();
        
        padre=(JFrame) parent;
        ficheros=fich;
        DefaultTableModel dtm= (DefaultTableModel) jTable1.getModel();
        
        for(int i=0;i<ficheros.size();i++)
        {
          Object[] row={ficheros.get(i),false};
          dtm.insertRow(i, row);
        }
       
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        _btn_AddVHDL = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Selección de fichero VHDL como Top");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "VHDL", "Top"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setColumnSelectionAllowed(true);
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jTable1.getColumnModel().getColumn(0).setResizable(false);
        jTable1.getColumnModel().getColumn(0).setPreferredWidth(300);
        jTable1.getColumnModel().getColumn(1).setResizable(false);
        jTable1.getColumnModel().getColumn(1).setPreferredWidth(30);

        jButton1.setText("Ok");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        _btn_AddVHDL.setText("Añadir VHDL");
        _btn_AddVHDL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _btn_AddVHDLActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 373, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(_btn_AddVHDL))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(239, 239, 239)
                        .addComponent(jButton1)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(_btn_AddVHDL)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(jButton1)))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

 
    int selecciones=0;
    int pos=0;
    DefaultTableModel dtm= (DefaultTableModel) jTable1.getModel();
    int i;
    for(i=0;i<ficheros.size();i++)
    {
        if((Boolean)dtm.getValueAt(i,1))
        {    
            selecciones ++;
            pos=i;
        }
    }
    
    if(selecciones ==1)
    {
        ((GUIPrincipal)padre).setTop(pos);
        this.dispose();
    }
    else
        if(selecciones >1)
            JOptionPane.showMessageDialog(this, "Elija solo un fichero como Top" +
                    " VHDL", "Error", JOptionPane.ERROR_MESSAGE);
        else//GEN-LAST:event_jButton1ActionPerformed
           JOptionPane.showMessageDialog(this, "Debe seleccionar un fichero como Top" +
                    " VHDL", "Error", JOptionPane.ERROR_MESSAGE);
    
}
private void _btn_AddVHDLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__btn_AddVHDLActionPerformed

        JFileChooser chooser;
        chooser = new JFileChooser();
        chooser.setMultiSelectionEnabled(true);
        Filtro filter = new Filtro("vhd");
        chooser.addChoosableFileFilter(filter);
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("Seleccionar Archivos VHDL");
        chooser.setAcceptAllFileFilterUsed(false);
        String fichero;
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            //try {
            File[] files; 
                   
            files=chooser.getSelectedFiles();
            
            for(int i=0; i<files.length;i++)
            {
                ficheros.add(files[i].getName());
                ((GUIPrincipal)padre).getFiles().add(files[i]);
               
            }
        }
        DefaultTableModel dtm= (DefaultTableModel) jTable1.getModel();
        int numFilas = dtm.getRowCount();
        for(int i=0;i<numFilas;i++)
        {
            dtm.removeRow(0);
        }
        
        for(int i=0;i<ficheros.size();i++)
        {
          Object[] row={ficheros.get(i),false};
          dtm.addRow( row);
        }
        
    
}//GEN-LAST:event__btn_AddVHDLActionPerformed

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    ((GUIPrincipal)padre).setCerradoTop(true);
}//GEN-LAST:event_formWindowClosing
 

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _btn_AddVHDL;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
